package com.tul.exercise.base.innerclass.chapter10_8.event;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 《java编程思想》10.8
 *  控制框架：温室控制器
 *
 **/
public class GreenhouseControls extends Controller{
    // 灯开关
    private boolean light = false;

    // 开灯事件
    public class LightOn extends Event{

        public LightOn(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            light = true;
        }

        public String toString() {
            return "Light is on";
        }
    }

    // 关灯事件
    public class LightOff extends Event{

        public LightOff(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            light = false;
        }

        public String toString() {
            return "Light is off";
        }
    }

    // 水开关
    private boolean water = false;

    // 开水事件
    public class WaterOn extends Event{

        public WaterOn(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            light = true;
        }

        public String toString() {
            return "Greenhouse water is on";
        }
    }

    // 关水事件
    public class WaterOnOff extends Event{

        public WaterOnOff(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            light = false;
        }

        public String toString() {
            return "Greenhouse water is off";
        }
    }

    private String thermostat  = "Day";
    public class ThermostatNight extends Event {

        public ThermostatNight(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            thermostat = "Night";
        }

        public String toString() {
            return "Thermostat on night setting";
        }
    }

    public class ThermostatDay extends Event {

        public ThermostatDay(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            thermostat = "Day";
        }

        public String toString() {
            return "Thermostat on day setting";
        }
    }

    public class Bell extends Event{

        public Bell(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            addEvent(new Bell(delayTime));
        }

        public String toString() {
            return "Bing!";
        }
    }

    public class Restart extends Event{
        private List<Event> eventList = new ArrayList<>();

        public Restart(long delayTime) {
            super(delayTime);
        }

        public Restart (long delayTime, List<Event> eventList) {
            super(delayTime);
            this.eventList = eventList;

            eventList.forEach(event -> {
                addEvent(event);
            });
        }

        @Override
        public void action() {

          eventList.forEach(event -> {
              event.start();
              addEvent(event);
          });

          start();
          addEvent(this);
        }

        public String toString() {
            return "Restarting system";
        }
    }

    public static class Terminate extends Event{

        public Terminate(long delayTime) {
            super(delayTime);
        }

        @Override
        public void action() {
            System.exit(0);
        }

        public String toString() {
            return "Terminating";
        }
    }

    public static void main(String[] args) {
        GreenhouseControls gc = new GreenhouseControls();

        gc.addEvent(gc.new Bell(900));

        List<Event> list = new ArrayList<>();
        list .add(gc.new ThermostatNight(0));
        list.add(gc.new LightOn(200));
        list.add(gc.new LightOff(400));
        list.add(gc.new WaterOn(600));
        list.add(gc.new WaterOnOff(800));
        list.add(gc.new ThermostatDay(1400));

        gc.addEvent(gc.new Restart(2000, list));

        if(args.length == 1) {
            gc.addEvent(new GreenhouseControls.Terminate(new Integer(args[0])));
        }
        gc.run();
    }
}
